package com.tanhua.server.controller;

import com.tanhua.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    /**
     * 个人信息查询
     * @param userId
     * @param token
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userId, @RequestHeader("Authorization")String token){

        //从ThreadLocal中获取用户id
        Long id = UserHolder.getUserId();

        if (userId == null){
            userId = id;
        }
        UserInfoVo userInfo = userInfoService.getById(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 用户信息更新
     * @param userInfo
     * @param token
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(UserInfo userInfo,@RequestHeader("Authorization")String token){
//从ThreadLocal中获取用户id
        Long id = UserHolder.getUserId();
        userInfo.setId(id);
        userInfoService.updateUserInfo(userInfo);
        return ResponseEntity.ok().body(null);

    }
}
